<template>
  <div class="warpper">
    <van-search
      class="my-search"
      v-model="keywords"
      placeholder="请输入搜索关键词"
      @keydown.enter="searchSong"
    ></van-search>
    <div class="content">
      <div class="wrap">
        <div class="title">单曲</div>
        <div
          class="playlist-item"
          v-for="item in resultData"
          :key="item.id"
          @click="selectPlayList(item)"
        >
          <div class="playlist-item-info">
            <div class="info-name">{{ item.name }}</div>
            <div class="info-other">
              <span>
                <span>{{ item.artists[0].name }}</span>
              </span>
              -
              <span>{{ item.album.name }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      keywords: "",
      resultData: [],
    };
  },
  created() {
    this.getResult();
  },
  methods: {
    getResult() {
      this.keywords = this.$route.params.keywords,
        axios
          .get("/api/search/?keywords=" + this.keywords)
          .then((res) => {
            console.log(res.data);
            this.resultData = res.data.result.songs;
          })
          .catch((err) => {
            console.error(err);
          });
    },
    refreshResult() {
      axios
        .get("/api/search/?keywords=" + this.keywords)
        .then((res) => {
          console.log(res.data);
          this.resultData = res.data.result.songs;
        })
        .catch((err) => {
          console.error(err);
        });
    },
    searchSong() {
      if (this.keywords == "") {
        alert("搜索关键字不能为空");
      } else {
        this.refreshResult();
      }
    },
    selectPlayList(item){
      const id = item.id;
      this.$router.push({ name: "Play",
      params:{id:id}
      });
    }
  },
};
</script>
<style scoped>
.my-search {
  padding: 15px 0;
  width: 94%;
  margin: 0 auto;
}
.my-search > .van-search__content {
  border-radius: 30px;
}
.content {
  text-align: left;
}
.wrap {
  background: #fafafa;
  margin: 20px;
  padding: 20px;
  border-radius: 10px;
}
.title {
  padding-bottom: 10px;
  border-bottom: 1px #e6e6e6 solid;
  font-weight: 900;
}
.playlist-item {
  text-align: left;
  padding: 8px 0;
  border-bottom: 1px #e6e6e6 solid;
}
.playlist-item-info {
  width: 90%;
  display: inline-block;
}
.info-name {
  margin-bottom: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.info-other {
  color: #878787;
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>